package com.logic.landseaserver.service.bill.offrent;

import com.logic.landseaserver.common.enums.BillTypeEnum;
import com.logic.landseaserver.common.util.DateUtil;
import com.logic.landseaserver.common.util.LandSeaUtil;
import com.logic.landseaserver.domain.BillDetail;
import com.logic.landseaserver.domain.Contract;
import com.logic.landseaserver.domain.PreOffrentDetailBill;
import org.apache.commons.lang.time.DateUtils;

import java.util.Date;

/**
 * Created by Junny on 2017/8/22.
 * 退房房租处理
 */
public class OffrentPropertyBillFactory implements OffRentBillFactory {

    /**
     * 合同信息
     */
    private Contract contract;

    private Date startDate;

    private Date endDate;

    private Double billAmount;

    private Integer offrentFlag;

    private BillDetail serviceDetail;

    private Date offRentDate;

    private Double avrMonthDays;

    public OffrentPropertyBillFactory(Contract contract, BillDetail serviceDetail,Double avrMonthDays){
        //一个月房租
        this.billAmount = contract.getPropertyFee();
        this.contract = contract;
        this.avrMonthDays=avrMonthDays;
        //退租日期
        this.offRentDate = new Date();
        //退租日期和缴费日期比较
        this.offrentFlag=DateUtil.compareTime(offRentDate,serviceDetail.getDateEnd());
        this.serviceDetail = serviceDetail;
        //已缴费到之后日期 退还给租客
        if(offrentFlag==-1){
            this.startDate = serviceDetail.getDateBegin();
        }else if(offrentFlag==1){
            //未缴费
            this.startDate = DateUtils.addDays(serviceDetail.getDateEnd(),1);
        }
        this.endDate = offRentDate;
    }
    private Double calculationAmount(){
        int diffMonth=DateUtil.getInstence().getApartMonth(this.startDate,this.endDate);
        int dayBefore = 0;
        int dayAfter = 0;
        //start月的金额
        Double daysBfPrice=0.0;
        Double daysAfPrice=0.0;
        //获取startDate月的总天数
        //int dayOfBfMonth = DateUtil.getInstence().getDaysOfMouth(this.startDate);
        Double dayOfBfMonth = this.avrMonthDays;
        if(diffMonth==0){//不跨月
            dayBefore = DateUtil.getInstence().differentDaysByMillisecond(this.startDate,this.endDate);
        }else{//跨月
            //获取startDate月的总天数
            dayBefore = DateUtil.getInstence().differentDaysByMillisecond(this.startDate,DateUtil.getInstence().getLastDayOfMonth(this.startDate));
            //获取endDate月的总天数
           // int dayOfAtMonth = DateUtil.getInstence().getDaysOfMouth(this.endDate);
            Double dayOfAtMonth = this.avrMonthDays;
            dayAfter = DateUtil.getInstence().differentDaysByMillisecond(DateUtil.getInstence().getFirstDayOfMonth(this.endDate),this.endDate);
            daysAfPrice =  LandSeaUtil.get2Double(this.billAmount / dayOfAtMonth * dayAfter);
        }
        //start月的金额
        //如果是剩余天数=当月天数 则默认为1个月
        if( DateUtil.getInstence().getDaysOfMouth(this.startDate)==dayBefore){
            daysBfPrice = this.billAmount;
        }else{
            daysBfPrice = LandSeaUtil.get2Double(this.billAmount / dayOfBfMonth * dayBefore);
        }
        int betweenMonth = diffMonth-1<0 ? 0 :diffMonth-1;
        this.billAmount = LandSeaUtil.getIntDouble(daysBfPrice+(this.billAmount*betweenMonth)+daysAfPrice);
        //退还给租客
        if(offrentFlag==-1){
            this.startDate = DateUtils.addDays(this.offRentDate,1);
            this.endDate = this.serviceDetail.getDateEnd();
            if(DateUtil.compareTime(this.offRentDate,this.serviceDetail.getDateBegin())==-1){
                this.startDate =this.serviceDetail.getDateBegin();
                return -(this.serviceDetail.getMoney());
            }else{
                Double refundMoney =  -(this.serviceDetail.getMoney()-this.billAmount);
                if(refundMoney >= 0.0){//授权码后租客应缴
                    this.startDate =this.serviceDetail.getDateBegin();
                    this.endDate =this.offRentDate;
                }
                return refundMoney;
                // return -(this.hourseDetail.getMoney()-this.billAmount);
            }
        }
        //租客缴费
        else{//(offrentFlag==1)
            return this.billAmount;
        }
    }

    @Override
    public PreOffrentDetailBill createBill() {
        if(this.startDate!=null){
            PreOffrentDetailBill bill = new PreOffrentDetailBill();
            bill.setPreType(BillTypeEnum.CTBI9.getCode());
            bill.setBillAmount(this.calculationAmount());
            bill.setDateBegin(this.startDate);
            bill.setDateEnd(this.endDate);
            return bill;
        }else{
            return null;
        }
    }
}